package com.test.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.test.entity.Student;
import com.test.mapper.StudentMapper;
import com.test.service.StudentService;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service
public class StudentServiceImpl implements StudentService {
    @Resource
    StudentMapper studentMapper;
    @Resource
    RedisTemplate redisTemplate;
    private static final String REDIS_KEY_PREFIX = "student_page_"; // Redis key 前缀
    @Override
    @Cacheable(value = REDIS_KEY_PREFIX, key = "#currentPage + '_' + #pageSize")
    public Page<Student> pages(Integer currentPage,
                               Integer pageSize) {
        // 尝试从 Redis 中获取数据
        Page<Student> students=new Page<>(currentPage,pageSize);
            studentMapper.selectPage(students,null);
        return students;
    }

    @Override
    @CacheEvict(value = REDIS_KEY_PREFIX, allEntries = true) // 清空该缓存
    public void delete(Integer id) {
        studentMapper.deleteById(id);
//        Set<String> keys = redisTemplate.keys(REDIS_KEY_PREFIX + "*");
//        if (keys != null) {
//            redisTemplate.delete(keys); // 批量删除匹配的 key
//        }
    }
}
